package com.itwn.web;

import com.itwn.entity.User;
import com.itwn.service.UserService;
import com.itwn.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends BaseServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("username");
        String pass = req.getParameter("password");
        req.setCharacterEncoding("utf-8");
        UserService userService = (UserService) SpringUtil.getBean("userServiceImpl");
        User user = userService.queryByNameAndPass(name, pass);
        if (user==null){
            req.setAttribute("errorInfo","账号或者密码错误");
            req.getRequestDispatcher(req.getContextPath()+"/index");
        }else {
            req.getSession().setAttribute("loginUser",user);
            resp.sendRedirect("/list");
        }
    }
}
